<template>
	<div class="t0" :style="duibistate ? 'width:200px;':'width:140px'">
		<div @click="collect(1)" v-if="isCollection == 0" class="t1">
			<img src="../assets/shou2.png" />
			<div>{{type == 1 ?'收藏房源':'收藏楼盘'}}</div>
		</div>
		<div @click="collect(2)" v-else class="t1">
			<img src="../assets/shou3.png" />
			<div>已收藏</div>
		</div>
		<!-- <div class="t1">
			<img src="../assets/fen1.png" />
			<div>分享房源</div>
		</div> -->
		<div class="t1" @click="contrast(1)" v-if="duibistate && !houseContrast">
			<img src="../assets/duibi.png" />
			<div>加入对比</div>
		</div>
		<div class="t1" @click="contrast(2)" v-if="duibistate && houseContrast">
			<img src="../assets/duibi.png" />
			<div>取消对比</div>
		</div>
	</div>
</template>

<script>
	export default{
		name:'collect',
		props:['type','bid'],
		data(){
			return{
				collectstate:false,
				duibistate:true,
				loginstate:false,
				isCollection:0,
				houseContrast:false,
			}
		},
		created() {
			this.getUser()
			if(this.type == 5){
				this.duibistate = false
			}
			console.log(this.bid)
		},
		methods:{
			//判断用户是否登录，如果登录则查询是否收藏
			getUser(){
				let user = JSON.parse(localStorage.getItem("userInfo"))
				if(user != null){
					this.loginstate = true
					this.$getHttp("/mob/user/getIsCollection",{userId:user.id,cbType:this.type,type:1,relationId:this.bid}).then((data) =>{
						if(data.code == 200){
							this.isCollection = data.isCollection
						}
					})
					//localStorage.setItem("userInfo",JSON.stringify(data.userInfo))
					const contrastIdList = JSON.parse(localStorage.getItem("contrastIdList"))
					if(contrastIdList != null){
						contrastIdList.forEach((e) =>{
							if(e == this.bid){
								this.houseContrast = true
							}
						})
					}
					console.log(contrastIdList)
				}else{
					this.loginstate = false
				}
			},
			//加入/取消对比
			contrast(type){
				if(this.loginstate){
					let contrastIdList = JSON.parse(localStorage.getItem("contrastIdList"))
					if(type == 1){
						// 超过四套不能对比
						if(contrastIdList&&contrastIdList.length>=4){
							this.$message({
								message: "最多4套进行对比",
								type: "error",
								offset: 120,
								duration: 2000,
							});
							return
						}
						//加入对比
						if(contrastIdList == null){
							let list = [this.bid]
							localStorage.setItem("contrastIdList",JSON.stringify(list))
						}else{
							contrastIdList.push(this.bid)
							localStorage.setItem("contrastIdList",JSON.stringify(contrastIdList))
						}
						this.houseContrast = true
						this.$message({
						  message: "已加入对比！",
						  type: "success",
						  offset: 120,
						  duration: 2000,
						});
					}else{
						//取消对比
						for (var i = 0; i < contrastIdList.length; i++) {
							if(contrastIdList[i] == this.bid){
								contrastIdList.splice(i,1)
							}
						}
						localStorage.setItem("contrastIdList",JSON.stringify(contrastIdList))
						this.houseContrast = false
						this.$message({
						  message: "已取消对比！",
						  type: "error",
						  offset: 120,
						  duration: 2000,
						});
					}
				}else{
					this.$message({
					  message: "请登录之后在对比房源！",
					  type: "error",
					  offset: 120,
					  duration: 2000,
					});
				}
			},
			addIntegral(val,title){
      this.$postHttp("/mob/iteration/addIntegral", {
				userId: JSON.parse(localStorage.getItem("userInfo")).id,
				ruleNumber:val,
        title:title
			})
			.then(res => {
			});
	  },
			collect(type){
				if(this.loginstate){
					if(type == 1){
						this.$postHttp("/mob/user/saveUserCollection",
						{
							userId:JSON.parse(localStorage.getItem("userInfo")).id,
							type:1,
							cbType:this.type,
							relationId:this.bid,
						}).then((data) =>{
							if(data.code == 200){
								this.isCollection = 1
								this.$message({
								  message: "收藏成功！",
								  type: "success",
								  offset: 120,
								  duration: 2000,
								});
							}
						})
						let title = this.type == 1 ?'收藏房源':'收藏楼盘';
						this.addIntegral(5,title)
					}else{
						this.$postHttp("/mob/user/saveUserCollection",
						{
							userId:JSON.parse(localStorage.getItem("userInfo")).id,
							type:1,
							cbType:this.type,
							relationId:this.bid,
							cancel:1
						}).then((data) =>{
							if(data.code == 200){
								this.isCollection = 0
								this.$message({
								  message: "取消收藏成功！",
								  type: "success",
								  offset: 120,
								  duration: 2000,
								});
							}
						})
					}
				}else{
					this.$message({
					  message: "请登录之后在收藏房源！",
					  type: "error",
					  offset: 120,
					  duration: 2000,
					});
				}
			},
		}
	}
</script>

<style lang="less" scoped>
.t0{
	height: 50px;
	display: flex;
	align-items: center;
	// justify-content: space-between;
	justify-content: flex-end;
}
.t1{
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	cursor: pointer;
	margin-left: 20px;
	img{
		width: 30px;
		height: 30px;
	}
	div{
		width: 60px;
		text-align: center;
		color: #333333;
		font-size: 14px;
	}
}
</style>
